$(document).ready(function(){
    $(".HeadDiv_Time").text(getThisTime());
    getWindowsHeight();

    /**入库**/
    $("#AddBut").click(function () {
        $(".AddMsg").show();
    });
    $("#AddMsg_But").click(function () {
        var Name=$("#Name").val()
        var Brand=$("#Brand option:selected").text()
        var Type=$("#Type option:selected").text()
        var Specifications=$("#Specifications option:selected").text()
        var Number=$("#Number").val()
        var Price=$("#Price").val()
        var Note=$("#Note").val()
        if(Name==""||Brand=="品牌"||Type=="类型"||Specifications=="规格"||Number==0||Price==0){
            layer.msg('请输入内容并选择内容');
            return;
        }
        Addaaa(Name,Brand,Type,Specifications,Number,Price,Note)
    });

    /**出库**/
    $("#OutBut").click(function () {
        var checkStatus = table.checkStatus('DataTable');
        AAA(checkStatus.data);
    });

    

    $(".AddMsg").click(function () {
        $(".AddMsg").hide();
    });
    $("#AddMsg_Content").click(function (e) {
        e.stopPropagation()
    });
    
    
    
    $("#recovery").click(function () {
        location.reload();
    });
});

const MyHead = new Vue({
    el: '#app',
    data: {
        data1: [],
        data2: [],
        data3: [],
        w:"",
        o:""
    },
    method:{

    }
});

/**获取其他数据**/
getUserMsg();
function getUserMsg() {
    axios({
        method: 'get',
        url:'/getOther',
    }).then(function (response) {
        MyHead.$data.data1=response.data.data1
        MyHead.$data.data2=response.data.data2
        MyHead.$data.data3=response.data.data3
    }).catch(function (error) {

    });;
}
/**获取出入库总额**/
wo();
function  wo() {
    axios({
        method: 'get',
        url:'/getWOPrice',
    }).then(function (response) {
        MyHead.$data.o=response.data.o;
        MyHead.$data.w=response.data.w;
    }).catch(function (error) {

    });;
}


/**获取时间**/
function getThisTime() {
    const ThisTime=new Date();
    const year = ThisTime.getFullYear();
    const month = ThisTime.getMonth() + 1;//月份是从0开始的
    const day = ThisTime.getDate();

    const myddy=ThisTime.getDay();//获取存储当前日期
    const weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    const newTime = year + "年" + month + "月" + day + "日  " + weekday[myddy];
    return newTime;
}
/**获取屏幕高**/
function getWindowsHeight() {
    var Wheight=$(window).height();
    $(".ContentDiv").css("height",(Wheight-140));
}

var table;
layui.use('table', function(){
    table = layui.table;
    table.render(op);
    //复选框事件
    table.on('checkbox(test)', function(obj){
        if(obj.checked){
            $("#OutBut").show();
        }else {
            $("#OutBut").hide();
        }
    });
    //单击框
    table.on('rowDouble(test)', function(obj){
        AAA(obj.data)
    });
});

var op={
    elem: '#DataTable'
    ,toolbar:true
    ,page: true //开启分页
    ,url:"/getAllAAA"
    ,cols: [[ //表头
        {field: 'id', title: 'ID', sort: true,type:"checkbox"}
        ,{field: 'name', title: '物品名'}
        ,{field: 'brand', title: '品牌'}
        ,{field: 'type', title: '类型'}
        ,{field: 'specifications', title: '规格'}
        ,{field: 'number', title: '数量', sort: true}
        ,{field: 'price', title: '金额'}
        ,{field: 'time', title: '时间'}
        ,{field: 'note', title: '备注'}
        ,{field: 'outBound', title: '是否出库',templet: function(d){
                return d.outBound?"<span style='color:red'>"+d.outBound+"</span>":"<span>"+d.outBound+"</span>"
            }}
    ]]
    ,parseData: function(res){ //res 即为原始返回的数据
        console.log(res)
        return {
            "code": "0", //解析接口状态
            "msg": "解析错误", //解析提示文本
            "count": res.length, //解析数据长度
            "data": res.AAAS //解析数据列表
        };
    }
}




/**获取表格选中数据进行入库**/
function AAA(Data) {
    layer.confirm('是否出库？', {
        btn: ['确定', '取消']
    }, function(index, layero){
        axios({
            method: 'post',
            url:'/setoutbound',
            data:{
                "id":Data.id
            }
            , headers: {'Content-Type': 'application/json'}
        }).then(function (response) {
            table.render(op);
        }).catch(function (error) {

        });;



        //确定回调
        layer.close(index);
    }, function(index){
        layer.close(index);
    });
}
/**添加**/
function Addaaa(Name,Brand,Type,Specifications,Number,Price,Note) {
    axios({
        method: 'post',
        url:'/AddAAA',
        data:{
            "Name":Name,
            "Brand":Brand,
            "Type":Type,
            "Specifications":Specifications,
            "Number":Number,
            "Price":Price*Number,
            "Note":Note
        },
        headers: {'Content-Type': 'application/json'}
    }).then(function (response) {
        console.log(response.data)
        if(response.data){
            $(".AddMsg").hide();
            $("#Name").val("")
            $("#Number").val("")
            $("#Price").val("")
            $("#Note").val("")
            layer.msg('添加成功');
        }else {
            layer.msg('添加失败');
        }
    }).catch(function (error) {
    });;
}